package com.zglc.ms.shop.service;

import com.zglc.ms.shop.dao.HomeTemplateDao;
import com.zglc.ms.shop.dao.HomeTemplateDetailDao;
import com.zglc.ms.shop.dao.ProductDao;
import com.zglc.ms.shop.dao.ProductPriceDao;
import com.zglc.ms.shop.model.ShopHomeTemplateDetailVo;
import com.zglc.ms.shop.model.ShopHomeTemplateVo;
import com.zglc.ms.shop.model.ShopProductVo;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by v.sc.d on 2017/4/12.
 */
@Service
@Transactional
public class ShopHomeTemplateVoService {

	@Autowired
	private HomeTemplateDao homeTemplateDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private HomeTemplateDetailDao homeTemplateDetailDao;

	@Autowired
	private ProductPriceDao productPriceDao;

	@Autowired
	private PointService pointService;

	public List<ShopHomeTemplateVo> getHomeTemplateByAll() {
		// 获取可显示的所有专区
		List<ShopHomeTemplateVo> shopHomeTemplates = homeTemplateDao.getShopHomeTemplateVoByAll();
		if (shopHomeTemplates == null) {
			return null;
		}
		List<ShopProductVo> pros = productDao.getShopProductVos();
		Map<Integer, String> ruleMap =pointService.getRuleMap();
		pros.stream().forEach(pro->pro.setPriceName(ruleMap.get(pro.getPriceType())));
		for (int i = 0; i < shopHomeTemplates.size(); i++) {
			// 遍历获取单个专区
			ShopHomeTemplateVo shopHomeTemplate = shopHomeTemplates.get(i);

			// 根据专区id获取专区内容
			List<ShopHomeTemplateDetailVo> homeTemplateDetails = homeTemplateDetailDao
					.getShopHomeTemplateDetailVoByTemplateIdAndGroup(shopHomeTemplate.getId());
			if (homeTemplateDetails == null || homeTemplateDetails.size() == 0) {
				continue;
			}

			boolean conFlag = false;
			for (int j = 0; j < homeTemplateDetails.size(); j++) {

				ShopHomeTemplateDetailVo shopHomeTemplateDetail = homeTemplateDetails.get(j);
				// 遍历专区内容，将三合一的专列进行内容填充
				if (shopHomeTemplateDetail.getTemplateProductType() == 1) {
					List<ShopHomeTemplateDetailVo> subHomeTemplateDetails = homeTemplateDetailDao
							.getShopHomeTemplateDetailVoByTemplateIdAndGroupId(shopHomeTemplate.getId(),
									shopHomeTemplateDetail.getDetailGroupId());
					if (subHomeTemplateDetails != null) {
						if (homeTemplateDetails.size() == 1 && subHomeTemplateDetails.size() != 3) {
							conFlag = true;
						}
						for (int x = 0; x < subHomeTemplateDetails.size(); x++) {
							ShopHomeTemplateDetailVo homeTemplateDetail = subHomeTemplateDetails.get(x);
							homeTemplateDetail
									.setShopProduct(getShopProductById(pros, homeTemplateDetail.getProductId()));
							subHomeTemplateDetails.set(x, homeTemplateDetail);
						}
						shopHomeTemplateDetail.setShopHomeTemplateDetails(subHomeTemplateDetails);
						homeTemplateDetails.set(j, shopHomeTemplateDetail);
					}
				} else {
					shopHomeTemplateDetail
							.setShopProduct(getShopProductById(pros, shopHomeTemplateDetail.getProductId()));
				}
			}
			if (conFlag) {
				continue;
			}
			if (homeTemplateDetails == null || homeTemplateDetails.size() == 0) {
				shopHomeTemplates.remove(i);
			} else {
				shopHomeTemplate.setHomeTemplateDetails(homeTemplateDetails);
				shopHomeTemplates.set(i, shopHomeTemplate);
			}
		}

		shopHomeTemplates = removeNullList(shopHomeTemplates);

		return shopHomeTemplates;
	}

	public ShopProductVo getShopProductById(List<ShopProductVo> shopProducts, int id) {
		for (ShopProductVo sp : shopProducts) {
			if (sp.getId() == id) {
				sp.setPriceList(productPriceDao.list(id));
				return sp;
			}
		}
		return null;
	}

	public List<ShopHomeTemplateVo> removeNullList(List<ShopHomeTemplateVo> removeShopHomeTemplates) {
		for (int i = 0; i < removeShopHomeTemplates.size(); i++) {
			ShopHomeTemplateVo shopHomeTemplate = removeShopHomeTemplates.get(i);
			if (ObjectUtils.equals(null, shopHomeTemplate.getHomeTemplateDetails())) {
				removeShopHomeTemplates.remove(i);
				removeNullList(removeShopHomeTemplates);
			}
		}
		return removeShopHomeTemplates;
	}

}